//
//  ViewController.m
//  NamingApp
//
//  Created by Simper on 2023/11/7.
//

#import "ViewController.h"
#import "GDTUnifiedInterstitialAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import "GDTRewardVideoAd.h"
#import "NAManager.h"

@interface ViewController ()<GDTUnifiedInterstitialAdDelegate,BUMNativeExpressRewardedVideoAdDelegate>
@property (nonatomic, strong) GDTUnifiedInterstitialAd *interstitial;
@property (nonatomic, strong) BUNativeExpressFullscreenVideoAd *interstitialAdABU;
@property (nonatomic, strong) NSString *gDTInterstitialAdId;
@property (nonatomic, strong) NSString *cSJInterstitialAdId;
@property(nonatomic,assign) BOOL isShowCSJInterstitialAd;
@property(nonatomic,assign) BOOL isShowGDTInterstitialAd;
@property(nonatomic,assign) BOOL isShowInterstitialAd;

@property (nonatomic, strong) GDTRewardVideoAd *rewardVideoAd;
@property (nonatomic, strong) BUNativeExpressRewardedVideoAd *rewardedAd;

@property (nonatomic, strong) NSString *gDTVideoAdId;
@property (nonatomic, strong) NSString *cSJVideoAdId;
@property(nonatomic,assign) BOOL isShowCSJVideoAd;
@property(nonatomic,assign) BOOL isShowGDTVideoAd;
@property(nonatomic,assign) BOOL isShowVideoAd;

@end

@implementation ViewController

- (void)loadVideoAd{
//  NSLog(@"loadVideoAd22",_isShowCSJVideoAd?@"YES":@"no",_isShowCSJVideoAd?@"YES":@"no");
  if(_isShowCSJVideoAd&&_isShowGDTVideoAd){
    if(_isShowVideoAd){
      [self loadGDTVideoAd];
    }else{
      [self loadCSJVideoAd];
    }
    _isShowVideoAd = !_isShowVideoAd;
    return;
  }
  if(_isShowCSJVideoAd){
    [self loadCSJVideoAd];
  }
  if(_isShowGDTVideoAd){
    [self loadGDTVideoAd];
  }
}

- (void)handleVideoAdData{
  _isShowCSJVideoAd = YES;
  _isShowGDTVideoAd = NO;
  _isShowVideoAd = YES;
  
  NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  NSDictionary *adData = [userDefault objectForKey:@"adData"];
//  NSString *videoAdIntervalTime = [adData objectForKey:@"videoAdIntervalTime"];
  
  _gDTVideoAdId = [adData objectForKey:@"gDTVideoAdId"];
  if(!_gDTVideoAdId){
    _gDTVideoAdId = @"8020744212936426";
  }else{
    _isShowGDTVideoAd = [[adData objectForKey:@"isShowGDTVideoAd"] boolValue];

  }
  

  _cSJVideoAdId = [adData objectForKey:@"cSJVideoAdId"];
  if(!_cSJVideoAdId){
    _cSJVideoAdId = @"102525233";
  }else{
    _isShowCSJVideoAd = [[adData objectForKey:@"isShowCSJVideoAd"] boolValue];

  }
  
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
  [self handleInterstitialAdData];
  [self handleVideoAdData];
  
//  [self loadGDTVideoAd];

    // Do any additional setup after loading the view.
}

- (void) handleShowAdData:(NSDictionary*)adData{
  [self handleVideoAdData];
//  _isShowCSJVideoAd = [[adData objectForKey:@"isShowCSJVideoAd"] boolValue];
//  _isShowGDTVideoAd = [[adData objectForKey:@"isShowGDTVideoAd"] boolValue];
//
//  _gDTInterstitialAdId = [[adData objectForKey:@"gDTInterstitialAdId"] stringValue];
//  _gDTVideoAdId = [[adData objectForKey:@"gDTVideoAdId"] stringValue];
//
//  _cSJVideoAdId = [[adData objectForKey:@"cSJVideoAdId"] stringValue];
//  _cSJInterstitialAdId = [[adData objectForKey:@"cSJInterstitialAdId"] stringValue];

}


// 收到load成功回调后展示
#pragma mark - BURewardedVideoAdDelegate
- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  NSLog(@"nativeExpressRewardedVideoAdDidLoad");
//  [self.rewardedAd showAdFromRootViewController:self];

}

-(void)loadCSJVideoAd{

  BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];

//  model.userId = @"tag123";

  self.rewardedAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:_cSJVideoAdId rewardedVideoModel:model];

  self.rewardedAd.delegate = self;
  
  [self.rewardedAd loadAdData];
}
- (void)showCSJRewardVideoAd {
    if (self.rewardedAd) {
        [self.rewardedAd showAdFromRootViewController:self];
    }
}
- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  [self showCSJRewardVideoAd];

  NSLog(@"unifiedInterstitialFailToLoadAd2 nativeExpressRewardedVideoAdDidDownLoadVideo");

}
- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify {
  NSLog(@"unifiedInterstitialFailToLoadAd223Express");

  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@YES}];

}
- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{
  NSLog(@"unifiedInterstitialFailToLoadAd223%@",[error localizedDescription]);
  NSLog(@"unifiedInterstitialFailToLoadAd2234%ld",(long)[error code]);

  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@NO}];


}
- (void)nativeExpressRewardedVideoAdServerRewardDidFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError * _Nullable)error {
  NSLog(@"unifiedInterstitialFailToLoadAd2%@",[error localizedDescription]);
  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@NO}];


}
- (void)nativeExpressRewardedVideoAdViewRenderFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
  NSLog(@"unifiedInterstitialFailToLoadAd3%@",[error localizedDescription]);
  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@NO}];

}

- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{
  NSLog(@"unifiedInterstitialFailToLoadAd3333%@%ld",[error localizedDescription],(long)[error code]);
//  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@NO}];
}



- (void)loadGDTVideoAd
 {
     self.rewardVideoAd = [[GDTRewardVideoAd alloc] initWithPlacementId:_gDTVideoAdId];
//     self.rewardVideoAd.delegate = self;
     self.rewardVideoAd.videoMuted = NO; // 设置激励视频是否静音
     [self.rewardVideoAd loadAd];
 }

// 确认激励视频广告有效性之后，在合适的时机展示广告
- (void)playVideo {
    [self.rewardVideoAd showAdFromRootViewController:self];
}

- (void)gdt_rewardVideoAdDidLoad:(GDTRewardVideoAd *)rewardedVideoAd{
  [self playVideo];
}

- (void)gdt_rewardVideoAdDidRewardEffective:(GDTRewardVideoAd *)rewardedVideoAd info:(NSDictionary *)info{
  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@YES}];

}
- (void)gdt_rewardVideoAd:(GDTRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
  NSLog(@"unifiedInterstitialFailToLoadAd666%@%ld",[error localizedDescription],(long)[error code]);
  [NAManager emitEventWithName:VEDIO_DATA_ENVENT andPayload:@{@"isVideoRewardSuccess":@NO}];

}



- (void)handleInterstitialAdData{
  _isShowCSJInterstitialAd = NO;
  _isShowGDTInterstitialAd = NO;
  _isShowInterstitialAd = NO;
  
  NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  NSDictionary *adData = [userDefault objectForKey:@"adData"];
  NSString *videoAdIntervalTime = [adData objectForKey:@"videoAdIntervalTime"];
  
  _isShowGDTInterstitialAd = [[adData objectForKey:@"isShowGDTInterstitialAd"] boolValue];
  if(_isShowGDTInterstitialAd){
    _gDTInterstitialAdId = [adData objectForKey:@"gDTInterstitialAdId"];
    if(!_gDTInterstitialAdId){
      _gDTInterstitialAdId = @"1111676582";
    }
  }
  
  _isShowCSJInterstitialAd = [[adData objectForKey:@"isShowCSJInterstitialAd"] boolValue];

  if(_isShowCSJInterstitialAd){
    _cSJInterstitialAdId = [adData objectForKey:@"cSJInterstitialAdId"];
    if(!_cSJInterstitialAdId){
      _cSJInterstitialAdId = @"1050652855580392";
    }
  }
  
}

- (void)loadInterstitialAd{
  if(_isShowCSJInterstitialAd&&_isShowGDTInterstitialAd){
    if(_isShowInterstitialAd){
      [self loadGDTAd];
    }else{
      [self loadCSJAd];
    }
    _isShowInterstitialAd = !_isShowInterstitialAd;
    return;
  }
  if(_isShowCSJInterstitialAd){
    [self loadCSJAd];
  }
  if(_isShowGDTInterstitialAd){
    [self loadGDTAd];
  }
}





- (void)loadGDTAd {
    if (self.interstitial) {
        self.interstitial.delegate = nil;
    }
    self.interstitial = [[GDTUnifiedInterstitialAd alloc] initWithPlacementId:_gDTInterstitialAdId];
    self.interstitial.delegate = self;
    self.interstitial.videoMuted = YES; // 设置视频是否Mute
    [self.interstitial loadAd]; // 加载插屏半屏广告
}

- (void)showAd {
    if ([self.interstitial isAdValid]) {
        [self.interstitial presentAdFromRootViewController:self]; // 展示插屏半屏广告，建议在unifiedInterstitialRenderSuccess回调后再展示广告可能体验效果会更好
    }

}

- (void)unifiedInterstitialSuccessToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
  [self showAd];
}


- (void)unifiedInterstitialFailToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
  NSLog(@"unifiedInterstitialFailToLoadAd%@",[error localizedDescription]);
}



- (void)loadCSJAd{
    
    
  self.interstitialAdABU = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:_cSJInterstitialAdId];
    self.interstitialAdABU.delegate = self;
//    self.interstitialAdABU.mutedIfCan = YES;//静音
    
  [self.interstitialAdABU loadAdData];
}
- (void)showInterstitialAd {
    
  if (self.interstitialAdABU) {
     [self.interstitialAdABU showAdFromRootViewController:self];
  }
}

#pragma mark - <---ABUinterstitialAdDelegate--->
- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
  [self showInterstitialAd];
//回调后再去展示广告，可保证播放和展示效果
}

- (void)nativeExpressFullscreenVideoAdViewRenderFail:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error{
  
}
- (void)nativeExpressFullscreenVideoAdDidPlayFinish:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error{
  
}
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error{
  
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
